// This file required display_errors.js
/**
	@description: This function will force forms to send ajax request (select: .ajaxForm). "js/jquery.form" must be added before calling this function.
**/
function init_ajax_forms() {
	$('.ajaxForm').each(function () {
		var form_id = $(this).attr('id');
		var responseObj;
		// wait for the DOM to be loaded 
		$(document).ready(function() { 
			var options = { 
				url: $('#' + form_id).attr('action'),
				//clearForm :true,
				beforeSerialize:function () {
					$('#' + form_id + ' .empty').each( function() {
						$(this).val('');
					});
				},
				beforeSubmit: function() {
					hide_errors();
					return $('#' + form_id).check_required_fields();
				},
				success: function(data) {
					console.log('data');
					if($('#' + form_id + ' input:file').length > 0 && $(".uploadifyQueueItem").last().length > 0) { //If form has file input
						data=file_upload_handler(form_id,data); //Processes it differently than other fields
						console.log('bbb');
					} else {
						console.log('aaa');
						console.log($.param(data));
						//$('#' + form_id).clearForm();	
					}
				},
				error: function(data,type,status) { 
					responseObj=$.parseJSON(data);
					display_errors(responseObj);
				},
				complete:function() {
					$('#' + form_id + ' .invalid').first().focus();
				},
			};

			$('#' + form_id).ajaxForm(options); 
		}); 
	});
}

//Client side handling for empty ".required" inputs
$.fn.check_required_fields=function () {
	var form_id = $(this).attr('id');

	//Add required class to child input
	$('#' + form_id + ' div.required > :input').each( function() {
		$(this).addClass('required');
	});
	
	//Do empty check for each .required fields
	$('#' + form_id + ' .required').each( function() {
		if($(this).val()=='' || $(this).val()===$(this).attr('placeholder')) {
			var field_id=$(this).attr('id');
			var field_error='This field cannot be left blank.';
			show_error(field_id,field_error);
		}
	}).change(function() {
		if($(this).val()!='' || $(this).val()!=$(this).attr('placeholder')) {
			var field_id=$(this).attr('id');
			$('#' + field_id).removeClass('invalid');
			$('#' + field_id + 'Error').remove();
		}
	});
	
	if($('#' + form_id + ' .required').hasClass('invalid')) {
		$('#' + form_id + ' .invalid').first().focus();
		return false;
	} else {
		return true;
	}
}


/**
Clear Form Inputs
**/
$.fn.clearForm = function() {
	var form_id = $(this).attr('id');
	$('#' + form_id + ' :input').each(function() {
		var type = this.type;
		if (type == 'text' || type == 'password' || type == 'textarea' ||  type == 'hidden' ||  type == 'file') {
			$(this).val('');
			if($(this).attr('placeholder')) {
				$(this).addClass('empty');
			}
		} else if (type == 'checkbox' || type == 'radio') {
			this.checked = false;
		} else if (type == 'select-one' || type == 'select-multiple') {
			this.selectedIndex = -1;
		}
	});
}

$.fn.customAjaxForm = function(redirect_uri) {
	
	var beforeSerialize;
	var beforeSubmit;
	var success;
	var error;
	var complete;
	var form_id=$(this).attr('id');
	
	var options = { 
		url: $('#' + form_id).attr('action'),
		//clearForm :true,
		beforeSerialize:function () {
			$('#' + form_id + ' .empty').each( function() {
				$(this).val('');
			});
		},
		beforeSubmit: function() {
			hide_errors();
			return $('#' + form_id).check_required_fields();
		},
		success: function(data) {
			if($('#' + form_id + ' input:file').length > 0 && $(".uploadifyQueueItem").last().length > 0 ) { //If form has file input
				file_upload_handler(form_id,data); //Processes it differently than other fields
			}
			
			data=$.parseJSON(data);
			var query_string=$.param(data);
			console.log(redirect_uri+'?'+query_string);
			window.location=redirect_uri+'?'+query_string;
		},
		error: function(data,type,status) { 
			responseObj=$.parseJSON(data);
			display_errors(responseObj);
		},
		complete:function() {
			$('#' + form_id + ' .invalid').first().focus();
		},
	};
	
	$('#' + form_id).ajaxForm(options);
}